// 社区装修中页
//  MZY_COM_detailViewController.m
//  FullHouse
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_COM_detailViewController.h"
#import "NetworkingTool.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "MZY_COM_textModel.h"
#import "MZY_COM_sixPic.h"
#import "MZY_COM_threePic.h"
#import "MZY_COM_noPic.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define HORIZONTAL [UIScreen mainScreen].bounds.size.width / 414
#define VERTICAL [UIScreen mainScreen].bounds.size.height / 736

@interface MZY_COM_detailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *informationArr;
@property(nonatomic, retain)NSMutableArray *picArr;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *locationLabel;
@property(nonatomic, retain)UIImageView *picImageView;
@property(nonatomic, retain)UIImageView *faceImageView;
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *viewCountLabel;
@property(nonatomic, retain)UILabel *likeCountLabel;
@property(nonatomic, retain)UILabel *commmentCountLabel;
@property(nonatomic, retain)UILabel *detailLabel;
@property(nonatomic, retain)MBProgressHUD *hud;

@end

@implementation MZY_COM_detailViewController

- (void)dealloc
{
    [self.tableView release];
    [self.informationArr release];
    [self.picArr release];
    [self.titleLabel release];
    [self.locationLabel release];
    [self.picImageView release];
    [self.faceImageView release];
    [self.nameLabel release];
    [self.viewCountLabel release];
    [self.likeCountLabel release];
    [self.commmentCountLabel release];
    [self.detailLabel release];
    [self.hud release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createData];
    [self createView];
}

#pragma mark 获取数据
- (void)createData{
    NSString *urlStr = [NSString stringWithFormat:@"http://api.meilijia.com/app/collection_page?col_id=%@&tab=diary&page=1",self.idStr];
    [NetworkingTool networkingWithStrURL:urlStr type:GETType bodyStr:nil block:^(id result) {
        self.informationArr = [NSMutableArray array];
        NSDictionary *dic = result[@"data"][@"collection_info"][@"info"];
        MZY_COM_textModel *text = [[MZY_COM_textModel alloc]init];
        [text setValuesForKeysWithDictionary:dic];
        [self.informationArr addObject:text];
        self.titleLabel .text = text.col_name;
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:25];
        self.locationLabel.text = text.title_sub_3;
        self.locationLabel.textColor = [UIColor whiteColor];
        self.locationLabel.textAlignment = NSTextAlignmentCenter;
        self.locationLabel.font = [UIFont systemFontOfSize:18];
        if ([text.cover isEqualToString:@""]) {
            self.picImageView.image = [UIImage imageNamed:@"full house.jpg"];
        }else{
            [self.picImageView sd_setImageWithURL:[NSURL URLWithString:text.cover]];
        }
        [self.faceImageView sd_setImageWithURL:[NSURL URLWithString:text.author_face]];
        self.faceImageView.layer.cornerRadius = 20;
        self.faceImageView.layer.masksToBounds = YES;
        self.nameLabel.text = text.author_nick;
        self.nameLabel.textColor = [UIColor whiteColor];
        self.viewCountLabel.text = text.view_count;
        self.viewCountLabel.textColor = [UIColor whiteColor];
        self.viewCountLabel.font = [UIFont systemFontOfSize:13];
        self.likeCountLabel.text = text.liked_count;
        self.likeCountLabel.textColor = [UIColor whiteColor];
        self.likeCountLabel.font = [UIFont systemFontOfSize:13];
        self.commmentCountLabel.text = text.comment_count;
        self.commmentCountLabel.textColor = [UIColor whiteColor];
        self.commmentCountLabel.font = [UIFont systemFontOfSize:13];
        
        self.picArr = [NSMutableArray array];
        for (NSDictionary *temp in result[@"data"][@"collection_diaries"][@"diaries"]) {
            MZY_COM_textModel *pic = [[MZY_COM_textModel alloc]init];
            [pic setValuesForKeysWithDictionary:temp];
            [self.picArr addObject:pic];
        }
        self.hud.hidden = YES;
        [self.tableView reloadData];
    }];
}

#pragma mark 创建界面
- (void)createView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    [self.view addSubview:headView];
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    [headView addSubview:self.picImageView];
    [self.picImageView release];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, WIDTH, 60)];
    [self.picImageView addSubview:self.titleLabel];
    [self.titleLabel release];
    self.locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, WIDTH, 30)];
    [self.picImageView addSubview:self.locationLabel];
    [self.locationLabel release];
    self.faceImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 40, 40)];
    [self.picImageView addSubview:self.faceImageView];
    [self.faceImageView release];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 260, 150, 20)];
    [self.picImageView addSubview:self.nameLabel];
    [self.nameLabel release];
    self.commmentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 30, 260, 20, 20)];
    [self.picImageView addSubview:self.commmentCountLabel];
    [self.commmentCountLabel release];
    UIImageView *commentImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 55, 260, 20, 20)];
    commentImageView.image = [UIImage imageNamed:@"iconfont-20151209tubiaolianxizhuanhuan21.png"];
    [self.picImageView addSubview:commentImageView];
    [commentImageView release];
    self.likeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 85, 260, 20, 20)];
    [self.picImageView addSubview:self.likeCountLabel];
    [self.likeCountLabel release];
    UIImageView *likeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 110, 260, 20, 20)];
    likeImageView.image = [UIImage imageNamed:@"iconfont-zan.png"];
    [self.picImageView addSubview:likeImageView];
    [likeImageView release];
    self.viewCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 160, 260, 40, 20)];
    [self.picImageView addSubview:self.viewCountLabel];
    [self.viewCountLabel release];
    UIImageView *viewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 185, 260, 20, 20)];
    viewImageView.image = [UIImage imageNamed:@"iconfont-liulan.png"];
    [self.picImageView addSubview:viewImageView];
    [viewImageView release];
    
    // 创建"tableView"
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor yellowColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 400;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = headView;
    [self.tableView registerClass:[MZY_COM_sixPic class] forCellReuseIdentifier:@"six"];
    [self.tableView registerClass:[MZY_COM_threePic class] forCellReuseIdentifier:@"three"];
    [self.tableView registerClass:[MZY_COM_noPic class] forCellReuseIdentifier:@"no"];
    [self.tableView release];
    [headView release];
    
    // 加载动画
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

#pragma mark "tableView"的第一个必须执行的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.picArr.count;
}

#pragma mark "tableView"的第二个必须执行的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MZY_COM_textModel *pic = self.picArr[indexPath.row];
    if (pic.pics.count == 0) {
        MZY_COM_noPic *cell = [tableView dequeueReusableCellWithIdentifier:@"no" forIndexPath:indexPath];
        self.tableView.rowHeight = 200 * HORIZONTAL;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        MZY_COM_textModel *text = self.picArr[indexPath.row];
        cell.timeLabel.text = text.post_date;
        cell.timeLabel.layer.cornerRadius = 5;
        cell.timeLabel.layer.masksToBounds = YES;
        cell.timeLabel.textColor = [UIColor whiteColor];
        cell.timeLabel.textAlignment = NSTextAlignmentCenter;
        cell.typeLabel.text = text.section_name;
        cell.typeLabel.textColor = [UIColor whiteColor];
        cell.typeLabel.layer.cornerRadius = 5;
        cell.typeLabel.layer.masksToBounds = YES;
        cell.typeLabel.textAlignment = NSTextAlignmentCenter;
        cell.introduceLabel.text = text.content;
        cell.introduceLabel.font = [UIFont systemFontOfSize:17 * HORIZONTAL];
        cell.introduceLabel.numberOfLines = 0;
        [cell.introduceLabel sizeToFit];
        cell.commentImageView.image = [UIImage imageNamed:@"iconfont-20151209tubiaolianxizhuanhuan11-2.png"];
        cell.commentCountLabel.text = text.section_value;
        cell.commentCountLabel.textColor = [UIColor lightGrayColor];
        return cell;
    }else if (pic.pics.count > 0 && pic.pics.count <= 3){
        MZY_COM_threePic *cell = [tableView dequeueReusableCellWithIdentifier:@"three" forIndexPath:indexPath];
        self.tableView.rowHeight = 340 * HORIZONTAL;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        MZY_COM_textModel *text = self.picArr[indexPath.row];
        cell.timeLabel.text = text.post_date;
        cell.timeLabel.layer.cornerRadius = 5;
        cell.timeLabel.layer.masksToBounds = YES;
        cell.timeLabel.textColor = [UIColor whiteColor];
        cell.timeLabel.textAlignment = NSTextAlignmentCenter;
        cell.typeLabel.text = text.section_name;
        cell.typeLabel.textColor = [UIColor whiteColor];
        cell.typeLabel.layer.cornerRadius = 5;
        cell.typeLabel.layer.masksToBounds = YES;
        cell.typeLabel.textAlignment = NSTextAlignmentCenter;
        cell.introduceLabel.text = text.content;
        cell.introduceLabel.font = [UIFont systemFontOfSize:17 * HORIZONTAL];
        cell.introduceLabel.numberOfLines = 0;
        [cell.introduceLabel sizeToFit];
        if (text.pics.count == 1) {
            [cell.leftTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[0][@"pic"]]];
            cell.midTopImageView.image = [UIImage imageNamed:@"iconfont-kongbai-2.png"];
            cell.rightTopImageView.image = [UIImage imageNamed:@"iconfont-kongbai-2.png"];
        }else if (text.pics.count == 2){
            [cell.leftTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[0][@"pic"]]];
            [cell.midTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[1][@"pic"]]];
            cell.rightTopImageView.image = [UIImage imageNamed:@"iconfont-kongbai-2.png"];
        }else{
            [cell.leftTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[0][@"pic"]]];
            [cell.midTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[1][@"pic"]]];
            [cell.rightTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[2][@"pic"]]];
        }
        cell.commentImageView.image = [UIImage imageNamed:@"iconfont-20151209tubiaolianxizhuanhuan11-2.png"];
        cell.commentCountLabel.text = text.section_value;
        cell.commentCountLabel.textColor = [UIColor lightGrayColor];

        return cell;
    }else{
        MZY_COM_sixPic *cell = [tableView dequeueReusableCellWithIdentifier:@"six" forIndexPath:indexPath];
        self.tableView.rowHeight = 460 * HORIZONTAL;
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        MZY_COM_textModel *text = self.picArr[indexPath.row];
        cell.timeLabel.text = text.post_date;
        cell.timeLabel.layer.cornerRadius = 5;
        cell.timeLabel.layer.masksToBounds = YES;
        cell.timeLabel.textColor = [UIColor whiteColor];
        cell.timeLabel.textAlignment = NSTextAlignmentCenter;
        cell.typeLabel.text = text.section_name;
        cell.typeLabel.textColor = [UIColor whiteColor];
        cell.typeLabel.layer.cornerRadius = 5;
        cell.typeLabel.layer.masksToBounds = YES;
        cell.typeLabel.textAlignment = NSTextAlignmentCenter;
        cell.introduceLabel.text = text.content;
        cell.introduceLabel.font = [UIFont systemFontOfSize:17 * HORIZONTAL];
        cell.introduceLabel.numberOfLines = 0;
        [cell.introduceLabel sizeToFit];
        if (text.pics.count == 4) {
            [cell.leftTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[0][@"pic"]]];
            [cell.midTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[1][@"pic"]]];
            [cell.rightTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[2][@"pic"]]];
            [cell.leftBottomImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[3][@"pic"]]];
            cell.midBottomImageView.image = [UIImage imageNamed:@"iconfont-kongbai-2.png"];
            cell.rightBottomImageView.image = [UIImage imageNamed:@"iconfont-kongbai-2.png"];
        }else if (text.pics.count == 5){
            [cell.leftTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[0][@"pic"]]];
            [cell.midTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[1][@"pic"]]];
            [cell.rightTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[2][@"pic"]]];
            [cell.leftBottomImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[3][@"pic"]]];
            [cell.midBottomImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[4][@"pic"]]];
            cell.rightBottomImageView.image = [UIImage imageNamed:@"iconfont-kongbai-2.png"];
        }else{
            [cell.leftTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[0][@"pic"]]];
            [cell.midTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[1][@"pic"]]];
            [cell.rightTopImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[2][@"pic"]]];
            [cell.leftBottomImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[3][@"pic"]]];
            [cell.midBottomImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[4][@"pic"]]];
            [cell.rightBottomImageView sd_setImageWithURL:[NSURL URLWithString:text.pics[5][@"pic"]]];
        }
        cell.commentImageView.image = [UIImage imageNamed:@"iconfont-20151209tubiaolianxizhuanhuan11-2.png"];
        cell.commentCountLabel.text = text.section_value;
        cell.commentCountLabel.textColor = [UIColor lightGrayColor];
        return cell;
    }
}

#pragma mark 返回按钮的点击方法
- (void)backAction:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
